﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ConsoleMenu;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Button button_return = new Button();
        Button button_submit_about = new Button();
        Panel panel2 = new Panel();
        RichTextBox about_show = new RichTextBox();

        public Form1()
        {
            InitializeComponent();
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            VedioFileService.Instance().RemoveAllFile();
            //OpenFileDialog openDlg = new OpenFileDialog();
            panel1.Controls.Clear();//清空全部的组件
            FolderBrowserDialog fbDialog = new FolderBrowserDialog();
            DialogResult result = fbDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                //Console.WriteLine(fbDialog.SelectedPath);
                //获取该目录下的所有视频文件
                DirectoryInfo rootInfo = new DirectoryInfo(fbDialog.SelectedPath);
                foreach (FileInfo fInfo in rootInfo.GetFiles("*.mp4"))
                {
                    VedioFileService.Instance().AddFile(new Vedio(fInfo.FullName, fInfo.Name, fInfo.Length));

                    //btn.Location = new System.Drawing.Point(132, 76); // 指定存放位置
                }
                VedioFileService.Instance().Save();
                MessageBox.Show("录入完毕");

                List<Vedio> files = VedioFileService.Instance().GetAllFiles();
                for (int i = 0; i < files.Count; i++)
                {
                    Button btn = new Button();
                    //btn.Size = new System.Drawing.Size(75, 23); // 指定按钮大小
                    btn.Name = files[i].Name;
                    btn.Text = i+1+"."+ files[i].Name;
                    btn.Width = 300;
                    btn.Height = 23;
                    btn.Location = new Point(0, i * 23);
                    btn.Click += new System.EventHandler(this.Buttons_Click);
                    panel1.Controls.Add(btn);
                }
                
                //openDlg.ShowDialog();
                //openDlg.Filter = "*.mp4|*.wmv|*.asf|*.mpeg";
                //OpenFileDialog openDlg = new OpenFileDialog();
                //WMPLib.IWMPMedia media = axWindowsMediaPlayer1.newMedia(openDlg.FileName);
                //axWindowsMediaPlayer1.currentPlaylist.appendItem(media);
                //axWindowsMediaPlayer1.Ctlcontrols.play();

                //panel1.Controls.Clear();//清空全部的组件
            }
        }

        private void Buttons_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;//当前被点击按钮
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            axWindowsMediaPlayer1.URL = "";
            Vedio file = VedioFileService.Instance().GetFile(btn.Name);
            MessageBox.Show("即将播放的是:"+file.Name+"\n此视频位于:" + file.Path+"\n此视频大小:"+file.Size/1000000+"MB");
            WMPLib.IWMPMedia media = axWindowsMediaPlayer1.newMedia(file.Path);
            axWindowsMediaPlayer1.currentPlaylist.clear();
            axWindowsMediaPlayer1.currentPlaylist.appendItem(media);
            //axWindowsMediaPlayer1.currentPlaylist.insertItem(0, media);
            axWindowsMediaPlayer1.Ctlcontrols.play();
            label2.Text="正在播放："+ btn.Name;
        }
        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void 删除列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();//清空全部的组件
            try
            {
                if (File.Exists("data.txt"))
                {
                    File.Delete("data.txt");
                }
            }
            catch
            {
                MessageBox.Show("删除失败");
            }
            VedioFileService.Instance().RemoveAllFile();
            axWindowsMediaPlayer1.currentPlaylist.clear();
            label2.Text = "已经清空了";
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
            label2.Visible = false;
            panel1.Visible = false;
            panel2.Width = 1000;
            panel2.Height = 500;
            axWindowsMediaPlayer1.Ctlcontrols.pause();
            axWindowsMediaPlayer1.Visible = false;

            button_return.Width = 300;
            button_return.Height = 150;
            button_return.Enabled = true;
            button_return.Visible = true;
            button_return.Enabled = true;
            button_return.Visible = true;
            //btn.Size = new System.Drawing.Size(75, 23); // 指定按钮大小
            button_return.Name = "button_return";
            button_return.Text = "返回到视频播放界面";
            button_return.Location = new Point(10, 50);

            button_submit_about.Width = 300;
            button_submit_about.Height = 150;
            button_submit_about.Enabled = true;
            button_submit_about.Visible = true;
            button_submit_about.Location = new Point(10, 230);
            button_submit_about.Name = "button_submit_about";
            button_submit_about.Text = "在右边添加信息然后点击我就可以保存";

            about_show.Width = 800;
            about_show.Height = 500;
            about_show.Enabled = true;
            about_show.Visible = true;
            about_show.Location = new Point(320, 50);
            about_show.Name = "about_show";
            about_show.Text = "日志信息";
            ReadFile();
            
            button_return.Click += new System.EventHandler(this.Button_return_Click);
            button_submit_about.Click += new System.EventHandler(this.Button_submit_Click);
            panel2.Controls.Add(button_return);
            panel2.Controls.Add(button_submit_about);
            panel2.Controls.Add(about_show);
            this.Controls.Add(panel2);
        }

        private void ReadFile()
        {
            string path = "日志.txt";
            FileStream myFs = new FileStream(path, FileMode.Open);
            StreamReader mySr = new StreamReader(myFs);
            string content = mySr.ReadToEnd();
            about_show.Text = content;
            mySr.Close();
            myFs.Close();
        }

        private void Button_submit_Click(object sender, EventArgs e)
        {
            string path = "日志.txt";
            FileStream myKs = new FileStream(path, FileMode.Create);
            StreamWriter mySw = new StreamWriter(myKs);
            mySw.Write(about_show.Text);
            mySw.Close();
            myKs.Close();
            ReadFile();
        }

        private void Button_return_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            label1.Visible = true;
            label2.Visible = true;
            panel1.Visible = true;
            axWindowsMediaPlayer1.Ctlcontrols.pause();
            axWindowsMediaPlayer1.Visible = true;
            Button button_return = (Button)sender;
            button_return.Enabled = false;
            button_return.Visible = false;
            button_submit_about.Enabled = false;
            button_submit_about.Visible = false;
            about_show.Enabled = false;
            about_show.Visible = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(Convert.ToInt32(axWindowsMediaPlayer1.playState)==3)
            {
                axWindowsMediaPlayer1.fullScreen = true;
            }
            else
            {
                MessageBox.Show("请先选择一个你喜欢的视频才能够放大哦！！");
            }
            
        }
    }
}
